//
//  AnimeNewsViewController.m
//  动漫之家
//
//  Created by 王少泽 on 16/12/1.
//  Copyright © 2016年 王少泽. All rights reserved.
//

#import "AnimeNewsViewController.h"
#import "MoreNewsViewController.h"
#import "XZBaseNetwork.h"
#import <SDWebImageManager.h>
#import "UIImageView+WebCache.h"
#import "NewsHtmlViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface AnimeNewsViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation AnimeNewsViewController
{
    UITableView* _newsTable;
    NSMutableArray* _dataSource;
    NSMutableArray* _newsSource;
    UIPageControl* _currentPage;
    NSTimer* _circleTime;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"动漫资讯";
    //添加侧栏按钮
    UIBarButtonItem* more = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"morenews"] style:UIBarButtonItemStyleDone target:self action:@selector(pushMoreNews:)];
    self.navigationItem.rightBarButtonItem = more;
    
    
    
    
    [self lazyTableView];
    [self.view addSubview:_newsTable];
    
    
    //进行第一次网络请求
    
    
    
    
}

- (void)startNetGetHeaderScroll
{
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://v2.api.dmzj.com/article/recommend/header.json?channel=ios&version=2.1.1" params:
     @{@"code":@"0",
       @"msg":@"成功",
       @"data":@"array"
       } success:^(id success) {
           
           _dataSource = [success objectForKey:@"data"];
           NSLog(@"%@",_dataSource);
           [_newsTable reloadData];
       } failure:^(id failure) {
           
       }];
}

- (void)startNetGetNews
{
    [[XZBaseNetwork shareInstance] startWithGetURL:@"http://v2.api.dmzj.com/article/list/v2/0/2/0.json?channel=ios&version=2.1.1" params:nil success:^(id success) {
        
        
    } failure:^(id failure) {
        
    }];
}

- (void)pushMoreNews:(UIBarButtonItem* )sender
{
    MoreNewsViewController* MNVC = [[MoreNewsViewController alloc]init];
    [self.navigationController pushViewController:MNVC animated:YES];
}


#pragma mark tableView的实现
/*
 
    lazy模式加载
 
 */
- (UITableView* )lazyTableView
{
    if (_newsTable == nil) {
        _newsTable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _newsTable.delegate = self;
        _newsTable.dataSource = self;
    }
    return _newsTable;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell* )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* ruse = @"ruse";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:ruse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ruse];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 200;
    }
    else
    {
        return 10                                                                                                                                                                                                                                            ;
    }
}


- (UIView* )tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {

        UIView* newsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)];
        newsView.backgroundColor = [UIColor purpleColor];
        
        return newsView;

    }

    return nil;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    for (id object_url in _dataSource) {
        
        NSString* str = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"object_url"];
        
        NSLog(@"测试的内容!:%@",str);
        NSLog(@"id内容:%@",object_url);
    }
    
    NewsHtmlViewController* nh = [[NewsHtmlViewController alloc]init];
    NSString* urlRequest = [[_dataSource objectAtIndex:indexPath.section] objectForKey:@"object_url"];
    NSURL* urlRe = [[_dataSource objectAtIndex:indexPath.row] objectForKey:@"object_url"];
    
    nh.newsURL = urlRequest;
    nh.nUrl = urlRe;
    [self.navigationController pushViewController:nh animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
